_Pragma("once");

#include <memory>
#include <sstream>
#include <iomanip>

#include "KeypadListener.h"
#include "Screen.h"
//#include "GuiContext.h"

class GuiContext;


/**
初始化三大层级
界面对象级＝initialize

界面显示级 beforeDisplay/afterDisplay

编辑界面级 setValue/saveValue
*/
class BasePage : public KeypadListener {
public:

    friend class GuiContext;


   /**
     * 允许0作为输入参数 效果同不初始化刷新频率
     * @param pageNum
     * @param mRefreshPeriod
     */
    BasePage(const int pageNum);

    virtual ~BasePage();

    virtual void initialize();

    /**
     * 是否显示 用于维护页面的显示及刷新频率
     * @param isVisible
     */
    void setVisible(const bool visible) {
        this->visible = visible;
        if (this->visible){
            beforeDisplay();
        } else {
            afterDisplay();
        }
        refreshPage();
    }

    virtual void beforeDisplay(){
    }

    virtual void afterDisplay()  {
//        currentBlinkIndex = 0;
//        activeEditting = nullptr;
    }

    /**
     * 根据当前状态生成页面并输出到屏幕
     */
    virtual void refreshPage() {
        if (visible) {
            paint();
        }
    }

    virtual void paint() = 0;

    int onKeyDown(const KeyCode& keyCode) override {
        return 0;
    }
    int onKeyUp(const KeyCode& keyCode) override {
        return 0;
    }
    int onKeySet(const KeyCode& keyCode) override {
        return 0;
    }
    int onKeyEnt(const KeyCode& keyCode) override {
        return 0;
    }

    int onOtherKey(const KeyCode& keyCode) override {
        return 0;
    }

    int getPageNumber() const {
        return pageNum;
    }

protected:

    std::ostringstream& resetStream() {
        strStream.flags(flags);
        strStream.width(width);
        strStream.str(StringUtility::EMPTY_STRING);

        return strStream;
    }

    std::ios::fmtflags flags;
    std::streamsize width;

    std::ostringstream strStream;

        /**
     * 当前页码
     */
    int pageNum = 0;
    bool visible = true;
    bool focus = false;
    static Screen& screen;
};

typedef std::shared_ptr<BasePage>  BasePageSharedPtr;
